package com.example.usagelock;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Toast;

import com.example.surfaceball.R;

public class MainSurfaceView extends SurfaceView implements Callback{
	
	ArrayList<Label> label;
	
	Navigator navigator;	
	PointF point;
	MotionEvent eventTemp;
	
	CircleEquation equ;
	Rect[] rects;
	
	int count;
	
	PointF center;
	
	
	Context context;
	
	public MyNewThread mThread;
	
	public MainSurfaceView(Context context) {
		super(context);
		
		// TODO Auto-generated constructor stub
		SurfaceHolder holder = this.getHolder();
		holder.addCallback(this);
		this.context = context;
		mThread = new MyNewThread(holder, context);
		
		label = new ArrayList<Label>();
		equ = new CircleEquation(360, 745, 270);
		
		rects = equ.GetRects(8, 100);		
		
		ImageManager imageManager = ImageManager.getInstance(context);
		
		label.add(new Label(context, rects[0].left, rects[0].top, rects[0].right, rects[0].bottom));
		imageManager.setImages(label.get(0), ImageType.Phone);
				
		label.add(new Label(context, rects[1].left, rects[1].top, rects[1].right, rects[1].bottom));
		imageManager.setImages(label.get(1), ImageType.Memo);
		
		label.add(new Label(context, rects[2].left, rects[2].top, rects[2].right, rects[2].bottom));
		imageManager.setImages(label.get(2), ImageType.Key);
		
		label.add(new Label(context, rects[3].left, rects[3].top, rects[3].right, rects[3].bottom));
		imageManager.setImages(label.get(3), ImageType.Pencil);
		
		label.add(new Label(context, rects[4].left, rects[4].top, rects[4].right, rects[4].bottom));
		imageManager.setImages(label.get(4), ImageType.Config);
		
		label.add(new Label(context, rects[5].left, rects[5].top, rects[5].right, rects[5].bottom));
		imageManager.setImages(label.get(5), ImageType.Heart);
		
		label.add(new Label(context, rects[6].left, rects[6].top, rects[6].right, rects[6].bottom));
		imageManager.setImages(label.get(6), ImageType.Glasses);
		
		label.add(new Label(context, rects[7].left, rects[7].top, rects[7].right, rects[7].bottom));
		imageManager.setImages(label.get(7), ImageType.Thunder);
		
		navigator = new Navigator(context, label.size(), 300, 685, 420, 805);
		navigator.setImage(R.drawable.star_fill);
		navigator.setImage(R.drawable.star_nothing);
		
		count = 0;
		
		this.setZOrderOnTop(true);
		holder.setFormat(PixelFormat.TRANSPARENT);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(!mThread.isAlive())
			mThread = new MyNewThread(holder, context);
		mThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		try {
			mThread.isRun = false;
			mThread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	class MyNewThread extends Thread{
		SurfaceHolder mHolder;
		Context mContext;
		Paint paint;	
		
		public boolean isRun = true;
		
		MyNewThread(SurfaceHolder surfaceHolder, Context context){
			this.mHolder = surfaceHolder;
			this.mContext = context;
		}
		
		public void run(){
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			Canvas canvas = null;
			while(isRun){
				canvas = mHolder.lockCanvas();
				if(canvas == null)	continue;
				try {
					synchronized(mHolder){
						canvas.drawColor(0, PorterDuff.Mode.CLEAR);
						
						count = navigator.draw(canvas, point, label, count);
						label.get(0).draw(canvas, point, count);
						label.get(4).draw(canvas, point, count);
						
						
						if(eventTemp !=null){
							if(eventTemp.getAction() == MotionEvent.ACTION_UP)
								count = 0;
							if(count>=1 && (eventTemp.getAction() == MotionEvent.ACTION_DOWN || eventTemp.getAction() == MotionEvent.ACTION_MOVE)){
								label.get(2).draw(canvas, point, count);
								label.get(3).draw(canvas, point, count);
								label.get(1).draw(canvas, point, count);
								label.get(5).draw(canvas, point, count);
								label.get(6).draw(canvas, point, count);
								label.get(7).draw(canvas, point, count);
							}
						}
					}
				} finally {
					mHolder.unlockCanvasAndPost(canvas);					
				}				
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		eventTemp = event;
		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN:
			point = new PointF(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_UP:
			if(navigator.checkPasswords()){
				Toast.makeText(context, "MATCH!!!", Toast.LENGTH_SHORT).show();
				((Activity)context).finish();
			}else{
				TakeCamera.TakePictureFront();
			}
			point = null;
			break;
		case MotionEvent.ACTION_MOVE:
			point = new PointF(event.getX(), event.getY());
			break;
		}
		return true;
	}	
}
